package net.elib.service;

import net.elib.entity.Book;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
/**
 * Created by IntelliJ IDEA.
 * User: ked
 * Date: 21.08.12
 * Time: 20:52
 * To change this template use File | Settings | File Templates.
 */
public class BookService extends JdbcDaoSupport{
    String query = null;

    public Book find(int id) {
        query = "select * from files where id = ?";

        try {
            Book book = (Book) getJdbcTemplate().queryForObject(query, new Object[] {id},
                new RowMapper() {
                    Book fl;
                    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                        fl = new Book();
                        fl.setId(rs.getInt(1));
                        fl.setFilename(rs.getString(2));
                        fl.setType(rs.getString(3));
                        fl.setFile(rs.getBytes(4));
                        fl.setBookName(rs.getString(5));
                        fl.setBookAuthor(rs.getString(6));
                        fl.setBookPublishing(rs.getString(7));
                        fl.setBookDescription(rs.getString(8));

                        return fl;
                    }
            });

            return book;
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public List<Book> listAll() {
        query = "select id, filename, bookName, bookAuthor, bookPublishing, bookDescription from books";

        try{
            List<Book> books = getJdbcTemplate().query(query, new BeanPropertyRowMapper(Book.class));

            return books;
        } catch(Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public void save(final Book book) {
        query = "insert into books (filename, type, file, bookName, bookAuthor, bookPublishing, bookDescription) values (?, ?, ?, ?, ?, ?, ?)";

        try {
            synchronized(this) {
                getJdbcTemplate().update(new PreparedStatementCreator() {

                    public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                        PreparedStatement statement = con.prepareStatement(query);
                        statement.setString(1, book.getFilename());
                        statement.setString(2, book.getType());
                        statement.setBytes(3, book.getFile());
                        statement.setString(4, book.getBookName());
                        statement.setString(5, book.getBookAuthor());
                        statement.setString(6, book.getBookPublishing());
                        statement.setString(7, book.getBookDescription());
                        return statement;
                    }
                });
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void delete(int id) {
        query = "delete from books where id = ?";

        try {
            getJdbcTemplate().update(query, new Object[] {id});
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
